package com.hh.controller;

import com.hh.dto.ResultDTO;
import com.hh.service.ApplyService;
import com.hh.until.Tool;
import com.hh.vo.ApplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/back/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;

    @GetMapping("/findAll")
    public ResultDTO findAll(ApplyVO applyVO) {
        applyVO.setName(Tool.nullToStr(applyVO.getName()));
        applyVO.setEmail(Tool.nullToStr(applyVO.getEmail()));
        applyVO.setEnable(Tool.nullToInt(applyVO.getEnable()));
        applyVO.setPage(Tool.strToPage(applyVO.getPage().toString()));
        applyVO.setLimit(Tool.strToLimit(applyVO.getLimit().toString()));

        ResultDTO resultDTO = applyService.findAll(applyVO);
        return resultDTO;
    }

    @GetMapping("/findById")
    public ResultDTO findById(ApplyVO applyVO) {
        applyVO.setId(Tool.strToInt(applyVO.getId().toString()));

        ResultDTO resultDTO = applyService.findById(applyVO);
        return resultDTO;
    }

    @PostMapping("/add")
    public ResultDTO add(ApplyVO applyVO) {
        applyVO.setName(Tool.nullToStr(applyVO.getName()));
        applyVO.setContent(Tool.nullToStr(applyVO.getContent()));
        applyVO.setPhone(Tool.nullToStr(applyVO.getPhone()));
        applyVO.setEmail(Tool.nullToStr(applyVO.getEmail()));
        applyVO.setEnable(0);

        ResultDTO resultDTO = applyService.add(applyVO);
        return resultDTO;
    }

    @PostMapping("/delete")
    public ResultDTO delete(ApplyVO applyVO) {
        applyVO.setId(Tool.strToInt(applyVO.getId().toString()));

        ResultDTO resultDTO = applyService.delete(applyVO);
        return resultDTO;
    }

    @PostMapping("/update")
    public ResultDTO update(ApplyVO applyVO) {
        applyVO.setId(Tool.strToInt(applyVO.getId().toString()));
        applyVO.setName(Tool.nullToStr(applyVO.getName()));
        applyVO.setContent(Tool.nullToStr(applyVO.getContent()));
        applyVO.setPhone(Tool.nullToStr(applyVO.getPhone()));
        applyVO.setEmail(Tool.nullToStr(applyVO.getEmail()));

        ResultDTO resultDTO = applyService.update(applyVO);
        return resultDTO;
    }

    @PostMapping("/reply")
    public ResultDTO reply(ApplyVO applyVO) {
        applyVO.setId(Tool.nullToInt(applyVO.getId()));
        applyVO.setReply(Tool.nullToStr(applyVO.getReply()));

        ResultDTO resultDTO = applyService.reply(applyVO);
        return resultDTO;
    }
}
